package com.ck.common.bean;

import com.ck.common.utils.enums.ResponseEnums;
import com.ck.common.utils.json.JacksonUtil;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;


/**
 * @author ck
 */
@Data
@NoArgsConstructor
public class ReturnResult<T> {
    /**
     * 返回成功
     */
    public static final String RST_SUCCESS = ResponseEnums.SUCCESS_OPTION.getCode();
    /**
     * 返回失败
     */
    public static final String RST_FAIL = ResponseEnums.SYSTEM_ERROR.getCode();

    public static final String RST_NETWORK_ERROR = "-98";
    public static final String RST_UNKNOWN_ERROR = "-99";


    /**
     * 返回值 0:成功 失败:会有各种值
     */
    private String rstCode;
    /**
     * 消息
     */
    private String rstMsg = "";
    /**
     * 返回参数
     */
    private T rstObj;


    /**
     * 返回成功
     *
     * @param rstObj
     */
    public ReturnResult(T rstObj) {
        rstCode = RST_SUCCESS;
        rstMsg = "success";
        this.rstObj = rstObj;
    }

    /**
     * 自定义初始化
     *
     * @param rstCode
     * @param rstMsg
     * @param rstObj
     */
    public ReturnResult(String rstCode, String rstMsg, T rstObj) {
        this.rstCode = rstCode;
        this.rstMsg = rstMsg;
        this.rstObj = rstObj;
    }

    /**
     * 自定义初始化
     *
     * @param resp
     * @param rstObj
     */
    public ReturnResult(ResponseEnums resp, T rstObj) {
        rstCode = resp.getCode();
        rstMsg = resp.getMsg();
        rstObj = rstObj;
    }

    /**
     * 初始化失败结果
     *
     * @param msg
     * @return
     */
    public static ReturnResult<String> fail(String msg) {
        return new ReturnResult<String>(RST_FAIL, msg, "");
    }


    /**
     * 初始化失败结果
     *
     * @param resp
     * @param msg
     * @return
     */
    public static ReturnResult<String> fail(ResponseEnums resp, String msg) {
        if (StringUtils.isEmpty(msg)) {
            msg = resp.getMsg();
        }

        return new ReturnResult<String>(resp.getCode(), msg, "");
    }

    /**
     * 初始化成功结果
     *
     * @param obj
     * @return
     */
    public static ReturnResult<Object> success(Object obj) {
        return new ReturnResult<Object>(obj);
    }


    /**
     * 构造错误信息
     *
     * @param error
     * @return
     */
    public static ReturnResult<String> buildErrorMsg(String error) {
        ReturnResult<String> rst;

        if (RST_NETWORK_ERROR.equals(error)) {
            rst = new ReturnResult<String>(error, "Network Error", "");
        } else {
            rst = new ReturnResult<String>(RST_UNKNOWN_ERROR, "Unknown Error", "");
        }

        return rst;
    }


    @Override
    public String toString() {
        return "ReturnResult{" +
                "rstCode='" + rstCode + '\'' +
                ", rstMsg='" + rstMsg + '\'' +
                ", rstObj=" + rstObj +
                '}';
    }


    /**
     * to json
     *
     * @return
     */
    public String toJson() {
        return JacksonUtil.obj2json(this);
    }
}
